<?php
class Money_Model_DbTable_Transactions extends Engine_Db_Table
{
  protected $_rowClass = 'Money_Model_Transaction';
  
 
  public function sendMoney($recipients, $values = array()){
      //type
      // 1 sender
      // 2 recipient
      // 3 popolnit'
      // 4 brat'
      
      $viewer = Engine_Api::_()->user()->getViewer();
      
      $sender = $this->createRow();
      $sender->setFromArray(array(
          'user_id' =>  $viewer->getIdentity(),
          'state' => 'okay',
          'gateway_id' => 6,
          'type' => 6,
          'body' => $values['body'],
          'amount' => -($values['amount']),
          'gateway_parent_transaction_id' => $recipients,
          'timestamp' => new Zend_Db_Expr('NOW()'),
          'currency' => Engine_Api::_()->getApi('settings', 'core')->getSetting('money.currency', 'USD')
      ));
      $sender->save();
      
      $recip = $this->createRow();
      $recip->setFromArray(array(
          'user_id' => $recipients,
          'state' => 'okay',
          'gateway_id' => 7,
          'type' => 7,
          'amount' => $values['amount'],
          'body' => $values['body'],
          'gateway_parent_transaction_id' => $viewer->getIdentity(),
          'timestamp' => new Zend_Db_Expr('NOW()'),
          'currency' => Engine_Api::_()->getApi('settings', 'core')->getSetting('money.currency', 'USD')
      ));
      $recip->save();
      
      $table = Engine_Api::_()->getDbtable('money', 'money');
      $table->updateMoney($recipients, $values['amount']);
      
      return $sender;
      
  }
  
  public function createTransaction(Core_Model_Item_Abstract $order, Core_Model_Item_Abstract $subject, Core_Model_Item_Abstract $poster){
      $moneyTable = Engine_Api::_()->getDbtable('money', 'money');
      
      $row = $this->createRow();
      $row->user_id = $subject->owner_id;
      $row->timestamp = date('Y-m-d H:i:s');
      $row->order_id = $order->order_id;
      $row->type = '10';
      $row->amount = $subject->amount;
      $row->currency = Engine_Api::_()->getApi('settings', 'core')->getSetting('money.currency', 'USD');
      $row->gateway_parent_transaction_id = $poster->getIdentity();
      $row->save();
      
      $moneyTable->setMoney($subject->owner_id, $subject->amount);
      

      $rowS = $this->createRow();
      $rowS->user_id = $poster->getIdentity();
      $rowS->timestamp = date('Y-m-d H:i:s');
      $rowS->order_id = $order->order_id;
      $rowS->type = '10';
      $rowS->amount = -($subject->amount);
      $rowS->currency = Engine_Api::_()->getApi('settings', 'core')->getSetting('money.currency', 'USD');
      $rowS->save();
      
      $moneyTable->setMoney($poster->getIdentity(), -($subject->amount));
      
      $order->state = 'complete';
      $order->save();
  }
  
  public function getBenefitStatus(User_Model_User $user = null)
  {
    // Get benefit setting
    $benefitSetting = Engine_Api::_()->getApi('settings', 'core')->getSetting('payment.benefit');
    if( !in_array($benefitSetting, array('all', 'some', 'none')) ) {
      $benefitSetting = 'all';
    }

    switch( $benefitSetting ) {
      default:
      case 'all':
        return true;
        break;

      case 'some':
        if( !$user ) {
          return false;
        }
        return (bool) $this->select()
          ->from($this, new Zend_Db_Expr('TRUE'))
          ->where('user_id = ?', $user->getIdentity())
          ->where('type = ?', 'payment')
          ->where('status = ?', 'okay')
          ->limit(1);
        break;

      case 'none':
        return false;
        break;
    }

    return false;
  }
  
}